﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Security.Cryptography;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using System.Net.Mail;
using System.IO;
using System.Configuration;
using ECCIT.Security;
using OgilvyOne.UKNow.Globalization;

public partial class en_FinderPwd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Form["emailT"] != null)
            {
                string emi = Request.Form["emailT"].ToString();
                string isok = PwdBud(emi);
                if (isok != null)
                {
                    bool issend = sendEmail(emi, isok);
                    Response.Write(issend.ToString());
                }
            }
        }
    }

    private bool sendEmail(string fromAs, string pwd)
    {
        return sendMail(fromAs, pwd);
    }

    private string PwdBud(string emil)
    {


        MemberBLL mbb = new MemberBLL((int)LanguageType.cn);
        IList<MemberModel> lmm = mbb.CheckEmail(emil);
        if (lmm.Count > 0)
        {

            lmm[0].Password = emil.Replace("@", "x").Replace(".", "_");
            string restr = lmm[0].Password;
            lmm[0].Password = MD5Crypto.GetMD5Hash(lmm[0].Password);
            if (mbb.Save(lmm[0]).Result)
            {
                return restr;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;

        }
    }

    public static bool sendMail(string toMail, string pwd)
    {
        StreamReader sr = null;
        try
        {

            MailMessage dim = new MailMessage();
            //new MailAddress("发件人邮箱", "发件人名称");
            dim.From = new MailAddress(ConfigurationManager.AppSettings["FromAddres"], ConfigurationManager.AppSettings["EmilName"] + pwd);
            dim.To.Add(toMail);//收件人
            dim.Subject = ConfigurationManager.AppSettings["EmilTitle"];//标题
            dim.Body = ConfigurationManager.AppSettings["EmilCont"] + pwd;
            dim.IsBodyHtml = true;
            dim.BodyEncoding = System.Text.Encoding.UTF8;
            dim.Priority = System.Net.Mail.MailPriority.Normal;
            SmtpClient smtpClient = new SmtpClient();
            //发件人密码
            smtpClient.Credentials = new System.Net.NetworkCredential(dim.From.Address, ConfigurationManager.AppSettings["smtpSendPwd"]);
            smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            //邮件发送服务器
            smtpClient.Host = ConfigurationManager.AppSettings["smtpClientHost"];

            smtpClient.Send(dim);
        }
        catch (Exception)
        {
            return false;
        }
        finally
        {
            if (sr != null)
            {
                sr.Close();
                sr.Dispose();
            }
        }
        return true;
    }
}